﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using RK.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.BillPlugIn.ZhongRun
{
    [Description("ZhonRun:调拨基础价格表表单插件"), HotUpdate]
    public class BasePrice : AbstractBillPlugIn
    {
        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);
            //var statuesObj = this.Model.BillBusinessInfo.GetBillStatusField().GetFieldValue(this.Model.DataObject);
            //var sta2 = this.Model.BusinessInfo.GetBillStatusField().GetFieldValue(this.Model.DataObject);
            //var billNo = this.Model.BillBusinessInfo.GetBillNoField().GetFieldValue(this.Model.DataObject);
            //var bil2 = this.Model.BusinessInfo.GetBillNoField().GetFieldValue(this.Model.DataObject);
            var mat = this.Model.BusinessInfo.GetField("F_MaterialId").GetFieldValue(this.Model.DataObject) as DynamicObject;
            var useOrg = this.Model.BusinessInfo.GetField("FUseOrgId").GetFieldValue(this.Model.DataObject) as DynamicObject;
            int matId = mat == null ? 0 : Convert.ToInt32(mat["Id"]);
            string matNumber = mat == null ? "" : mat["Number"].ToString();
            int orgId = useOrg == null ? 0 : Convert.ToInt32(useOrg["Id"]);
            string fieldKeys = "F_MaterialId.fnumber";
            string filterString = $"FDocumentStatus='C' and F_MaterialId={matId} and FUseOrgId={orgId}";
            var list = Api.ExecuteBillQuery(this.Context, this.Model.BusinessInfo.GetForm().Id, fieldKeys, filterString, fieldKeys, 2000);
            if (list.Count > 0)
            {
                this.View.ShowErrMessage($"物料编码:{matNumber}已经维护过价格，不须重复维护！");
                e.Cancel = true;
            }
        }
    }
}
